package com.breakfast.fun.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.data.entity.home.product.ProductCommentData;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品评论适配器
 * Created by yukai on 16/9/29.
 */

public class ProductCommentAdapter extends BaseListAdapter<ProductCommentData> {
    public ProductCommentAdapter(Context context, List<ProductCommentData> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_comment, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProductCommentData data = getItem(position);

        holder.mTvCommentContent.setText(data.getContent());
        if (data.getNickName().length() == 11) {
            String nickName = data.getNickName();
            String subString = nickName.substring(3, 7);
            String replaceName = nickName.replace(subString, "****");
            data.setNickName(replaceName);
        }
        holder.mTvCommentName.setText(data.getNickName());
        holder.mTvCommentTime.setText(data.getAddTime());
        holder.mTvCommentScore.setText(data.getAve() + "分");

        //如果有回复的话，就显示回复布局
        if (data.getIsReply() == 1) {
            holder.mTvCommentReply.setVisibility(View.VISIBLE);
            holder.mTvCommentReply.setText("客服回复:" + data.getReply().getContent());
        } else {
            holder.mTvCommentReply.setVisibility(View.GONE);
        }

        imageUtil.loadCircleImage(data.getUserPhoto(), holder.mIvCommentHead);
        return convertView;
    }

    public void load(List<ProductCommentData> datas) {
        mList.addAll(datas);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.iv_comment_head)
        ImageView mIvCommentHead;
        @Bind(R.id.tv_comment_name)
        TextView mTvCommentName;
        @Bind(R.id.tv_comment_time)
        TextView mTvCommentTime;
        @Bind(R.id.tv_comment_score)
        TextView mTvCommentScore;
        @Bind(R.id.tv_comment_content)
        TextView mTvCommentContent;
        @Bind(R.id.tv_comment_reply)
        TextView mTvCommentReply;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
